home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 26 web services / advancedws / sampleservice.asmx.vb < prev    next >
Encoding:
Text File  |  2002-03-19  |  8.8 KB  |  263 lines

  1. ' This web service contains a number of do-nothing methods
  2. ' whose purpose is to test advanced web service techniques,
  3. ' such as caching and soap extensions.
  4.  
  5. Imports System.Web.Services
  6. Imports System.Web.Services.Protocols
  7. Imports System.Net
  8. Imports System.Threading
  9. Imports System.Web.Security
  10. Imports System.Xml.Serialization
  11.  
  12. <WebService(Description:="A web service with methods for doing tests", _
  13.     Namespace:="http://www.vb2themax.com/")> _
  14. Public Class SampleService
  15.     Inherits System.Web.Services.WebService
  16.  
  17. #Region " Web Services Designer Generated Code "
  18.  
  19.     Public Sub New()
  20.         MyBase.New()
  21.  
  22.         'This call is required by the Web Services Designer.
  23.         InitializeComponent()
  24.  
  25.         'Add your own initialization code after the InitializeComponent() call
  26.  
  27.     End Sub
  28.  
  29.     'Required by the Web Services Designer
  30.     Private components As System.ComponentModel.IContainer
  31.  
  32.     'NOTE: The following procedure is required by the Web Services Designer
  33.     'It can be modified using the Web Services Designer.  
  34.     'Do not modify it using the code editor.
  35.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  36.         components = New System.ComponentModel.Container()
  37.     End Sub
  38.  
  39.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  40.         'CODEGEN: This procedure is required by the Web Services Designer
  41.         'Do not modify it using the code editor.
  42.         If disposing Then
  43.             If Not (components Is Nothing) Then
  44.                 components.Dispose()
  45.             End If
  46.         End If
  47.         MyBase.Dispose(disposing)
  48.     End Sub
  49.  
  50. #End Region
  51.  
  52.     ' This method is useful to test cache behavior.
  53.  
  54.     <WebMethod(CacheDuration:=10)> _
  55.     Function GetTime(ByVal arg As Integer) As Date
  56.         Me.Application("GetTimeCounter") = GetTimeCounter() + 1
  57.         Return Date.Now
  58.     End Function
  59.  
  60.     ' this method returns the number of times the GetTime
  61.     ' method has actually executed, so that you can check that
  62.     ' if the method is cached, it isn't even reached by ASP.NET
  63.  
  64.     <WebMethod(Description:="The number of times GetTime has been called")> _
  65.     Function GetTimeCounter() As Integer
  66.         Dim o As Object = Me.Application("GetTimeCounter")
  67.         If o Is Nothing Then
  68.             Return 0
  69.         Else
  70.             Return CInt(o)
  71.         End If
  72.     End Function
  73.  
  74.     ' These two methods are used to test session state persistence.
  75.  
  76.     <WebMethod(Enablesession:=True)> _
  77.     Function IncrementCounter() As Integer
  78.         Dim o As Object = Session("counter")
  79.         If o Is Nothing Then
  80.             Session.Add("counter", 1)
  81.         Else
  82.             Session("counter") = CInt(o) + 1
  83.         End If
  84.         Return CInt(Session("counter"))
  85.     End Function
  86.  
  87.     <WebMethod(Enablesession:=True)> _
  88.     Function GetSessionID() As String
  89.         Return Session.SessionID
  90.     End Function
  91.  
  92.     ' this method is used to simulate a length method call
  93.  
  94.     <WebMethod(Description:="A lengthy method")> _
  95.     Function LengthyMethodCall(ByVal seconds As Integer) As Integer
  96.         ' wait for the specified number of seconds.
  97.         System.Threading.Thread.Sleep(seconds * 1000)
  98.         ' return the argument multiplied by 1000.
  99.         Return seconds * 1000
  100.     End Function
  101.  
  102.     ' this method is marked with the OneWay attribute
  103.  
  104.     <WebMethod(), SoapDocumentMethod(OneWay:=True)> _
  105.     Sub OneWayLengthyMethodCall(ByVal seconds As Integer)
  106.         ' wait for the specified number of seconds.
  107.         System.Threading.Thread.Sleep(seconds * 1000)
  108.     End Sub
  109.  
  110.     ' this method shows how you can have a method that returns
  111.     ' an object that can be one of many concrete classes
  112.  
  113.     <WebMethod(), SoapRpcMethod(), SoapInclude(GetType(Invoice)), SoapInclude(GetType(PurchaseOrder))> _
  114.     Function GetDocument(ByVal docname As String) As Document
  115.         Select Case docname.ToLower
  116.             Case "invoice"
  117.                 Return New Invoice()
  118.             Case "purchaseorder"
  119.                 Return New PurchaseOrder()
  120.         End Select
  121.     End Function
  122.  
  123.     ' this method can be used to test Soap Exceptions
  124.  
  125.     <WebMethod()> _
  126.     Sub ThrowAnException()
  127.         Throw New NullReferenceException()
  128.     End Sub
  129.  
  130.     ' This is the Public variable that will receive the userInfo header.
  131.     Public userInfo As UserInfoHeader
  132.  
  133.     <WebMethod(), SoapHeader("userInfo", Required:=False)> _
  134.     Function GetClientTime() As String
  135.         ' Provide a default userInfo object if necessary.
  136.         If userInfo Is Nothing Then
  137.             userInfo = New UserInfoHeader()
  138.         End If
  139.  
  140.         Dim serverTime As Date = Date.Now.ToUniversalTime
  141.         Dim clientTime As Date = serverTime.AddHours(userInfo.TimeOffset)
  142.         ' Creates a CultureInfo object with proper locale information.
  143.         Dim ci As New System.Globalization.CultureInfo(userInfo.Culture)
  144.         ' Return the time formatted using client's formatting rules.
  145.         Return clientTime.ToString(ci)
  146.     End Function
  147.  
  148.     Public accountInfo As AccountInfoHeader
  149.  
  150.     <WebMethod(), SoapHeader("accountInfo")> _
  151.     Function ProtectedMethod() As Boolean
  152.         ' Check that credentials are ok, throw exception if not.
  153.         ValidateAccount()
  154.         ' Return a value.
  155.         Return True
  156.     End Function
  157.  
  158.     <WebMethod(), SoapHeader("accountInfo")> _
  159.     Function AnotherProtectedMethod() As Boolean
  160.         ' Check that credentials are ok, throw exception if not.
  161.         ValidateAccount()
  162.         ' Return a value.
  163.         Return True
  164.     End Function
  165.  
  166.     ' validate username and password.
  167.     Private Sub ValidateAccount()
  168.         ' Throw exception if missing header.
  169.         If accountInfo Is Nothing Then
  170.             Throw New SoapException("Missing account header", SoapException.ClientFaultCode)
  171.         End If
  172.  
  173.         ' Throw exception if header members aren't set.
  174.         If accountInfo.UserName = "" Or accountInfo.Password = "" Then
  175.             Throw New SoapException("Missing account info", SoapException.ClientFaultCode)
  176.         End If
  177.  
  178.         ' Retrieve the subscription level of this user.
  179.         Dim thisUserSubscriptionLevel As Integer
  180.         thisUserSubscriptionLevel = GetUserSubscriptionLevel(accountInfo.UserName, accountInfo.Password)
  181.         ' exit if credentials were invalid.
  182.         If thisUserSubscriptionLevel < 0 Then
  183.             Throw New SoapException("Unknown user", SoapException.ClientFaultCode)
  184.         End If
  185.  
  186.         ' Retrieve the name of the method that called this procedure.
  187.         Dim st As New System.Diagnostics.StackTrace(False)
  188.         Dim sf As System.Diagnostics.StackFrame = st.GetFrame(1)
  189.         Dim mb As System.Reflection.MethodBase = sf.GetMethod
  190.  
  191.         ' Retrieve the required subscription level for the calling method.
  192.         Dim requiredSubscriptionLevel As Integer
  193.         Select Case mb.Name
  194.             Case "ProtectedMethod"
  195.                 requiredSubscriptionLevel = 1
  196.             Case "AnotherProtectedMethod"
  197.                 requiredSubscriptionLevel = 2
  198.         End Select
  199.  
  200.         ' Throw exception if subscription level isn't sufficient.
  201.         If thisUserSubscriptionLevel < requiredSubscriptionLevel Then
  202.             Throw New SoapException("Insufficient subscription level", SoapException.ClientFaultCode)
  203.         End If
  204.         ' Exit regularly if everything is ok.
  205.     End Sub
  206.  
  207.     <WebMethod(), SoapCustomAuthentication(2), SoapHeader("accountInfo")> _
  208.     Function YetAnotherProtectedMethod() As Boolean
  209.         ' return a dummy value in this demo
  210.         Return True
  211.     End Function
  212.  
  213. End Class
  214.  
  215. Public Class Document
  216.     Public [Date] As Date
  217.     Public Number As Integer
  218. End Class
  219.  
  220. Public Class Invoice
  221.     Inherits Document
  222.     Public Total As Decimal
  223. End Class
  224.  
  225. Public Class PurchaseOrder
  226.     Inherits Document
  227.     Public AuthorizedBy As String
  228. End Class
  229.  
  230. Public Class UserInfoHeader
  231.     Inherits SoapHeader
  232.  
  233.     ' This object contains culture info for the user.
  234.     Public Culture As String = ""
  235.     ' this object contains the time difference from UTC time.
  236.     Public TimeOffset As Single = 0
  237.  
  238. End Class
  239.  
  240. Public Class AccountInfoHeader
  241.     Inherits SoapHeader
  242.  
  243.     Public UserName As String
  244.     Public Password As String
  245. End Class
  246.  
  247. Module AuthenticationFunctions
  248.  
  249.     ' Get user subscription level, or -1 if credentials are invalid.
  250.     Function GetUserSubscriptionLevel(ByVal username As String, ByVal password As String) As Integer
  251.         ' (A real application would use a database instead.)
  252.         If username = "JoeDoe" And password = "jdpwd" Then
  253.             Return 1
  254.         ElseIf username = "AnnSmith" And password = "aspwd" Then
  255.             Return 2
  256.         Else
  257.             ' unknown user or invalid credentials
  258.             Return -1
  259.         End If
  260.     End Function
  261. End Module
  262.  
  263.